import fs from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));

export const prompt = async (name: string, params?: any) => {
  let text = await fs.promises.readFile(
    join(__dirname, `../prompt/${name}.md`),
    "utf8"
  );
  if (params) {
    text = text?.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
      // 如果参数值是对象，转换为JSON字符串
      const value = params?.[key];
      if (typeof value === "object" && value !== null) {
        return JSON.stringify(value, null, 2);
      }
      return value ?? match;
    });
  }
  return text;
};
